﻿using System;
using System.Globalization;
using System.Windows.Media;
using System.Xml;
using System.Xml.Schema;
using Blackbird.Common.Utility;
using Blackbird.Core.Editable.Map;

namespace Blackbird.Core.Editable.Base {
    public abstract class BaseEditableMapNode : BaseEditableEntity, IEditableMapNode {
        public string AssemblyQualifiedTypeName {
            get { return GetType().AssemblyQualifiedName; }
        }

        public abstract ImageSource ImageSource { get; }

        #region IEditableMapNode Members
        
        private int _height;
        public int Height {
            get { return _height; }
            set {
                if (_height == value) {
                    return;
                }
                _height = value;
                InvokePropertyChanged("Height");
            }
        }

        private int _width;
        public int Width {
            get { return _width; }
            set {
                if (_width == value) {
                    return;
                }
                _width = value;
                InvokePropertyChanged("Width");
            }
        }

        private float _impedence;
        public float Impedence {
            get { return _impedence; }
            set {
                if (Math.Abs(_impedence - value) > 0.001) {
                    _impedence = value;
                    InvokePropertyChanged("Impedence");
                }
            }
        }

        private int _x;
        public int X {
            get { return _x; }
            set {
                if (_x != value) {
                    _x = value;
                    InvokePropertyChanged("X");
                }
            }
        }

        private int _y;
        public int Y {
            get { return _y; }
            set {
                if (_y != value) {
                    _y = value;
                    InvokePropertyChanged("Y");
                }
            }
        }

        public override void ReadXml(XmlReader reader) {
            reader.ReadStartElement("mapnode");
            X = SerializationUtility.ReadAttribute(reader, "x", 0);
            Y = SerializationUtility.ReadAttribute(reader, "y", 0);
            Width = SerializationUtility.ReadAttribute(reader, "width", 0);
            Height = SerializationUtility.ReadAttribute(reader, "height", 0);
            Impedence = SerializationUtility.ReadAttribute(reader, "impedence", 0);
            ReadXmlContent(reader);
            reader.ReadEndElement();
        }

        public override void WriteXml(XmlWriter writer) {
            writer.WriteStartElement("mapnode");
            writer.WriteAttributeString("x", X.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("y", Y.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("width", Width.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("height", Height.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("impedence", Impedence.ToString(CultureInfo.CurrentCulture));
            writer.WriteAttributeString("assemblyqualifiedtypename", AssemblyQualifiedTypeName);
            WriteXmlContent(writer);
            writer.WriteEndElement();
        }

        #endregion

    }
}